package model

import (
	"fmt"
	"nt-order/common"
	"nt-order/conf"
)

type OrderStaffList struct {
	Staff_id   string `json:"staff_id"`
	Staff_name string `json:"staff_name"`
}

func (that *OrderStaffList) Table() string {
	return "order_staff_list"
}

func (that *OrderStaffList) UserInfo(staffId string) OrderStaffList {
	db := conf.GetDb()
	sqlT := fmt.Sprintf("select staff_id,staff_name from %s where staff_id = ? limit 1", that.Table())
	info := OrderStaffList{}
	_, err := db.SQL(sqlT, staffId).Get(&info)
	if err != nil {
		panic(err)
	}

	return info
}

func (that *OrderStaffList) GetStaffName(staffId string) string {
	redisKey := common.GetCacheKey("ORDER_STAFF_List")
	redis := conf.GetRedis()

	exists := redis.Exists(redisKey)
	if exists == 0 {
		db := conf.GetDb()
		list := make([]OrderStaffList, 0)
		err := db.Find(&list)
		if err != nil {
			panic(err)
		}

		tt := make(map[string]string)
		for _, staffList := range list {
			if len(staffList.Staff_id) > 0 {
				tt[staffList.Staff_id] = staffList.Staff_name
			}
		}
		err = redis.HMset(redisKey, tt)
		if err != nil {
			panic(err)
		}
	}

	staffName, err := redis.Hget(redisKey, staffId)
	if err != nil {
		return ""
	}

	return staffName

}
